#pragma once
//----------------------------------------------------------------------------------------------------------------
//			Filename:		MatlabUtilityDebuger.h
//			  Author:		Adrian De Barro
//			    Date:		12/08/2015
//----------------------------------------------------------------------------------------------------------------
//			  Status: REFACTORING
//----------------------------------------------------------------------------------------------------------------
#include <string>
#include <vector>

#include "..\..\CollisionDetails\ShadeRec.h"
#include "..\Point\Point3D\Point3D.h"
#include "..\GeometricObjects\Objects.h"
#include "..\GeometricObjects\GeometricObjects.h"

using std::vector;
namespace Engine
{
	//----------------------------------------------------------------------------------------------------------------
	//			Class Name: MatlabUtilityDebiger
	//		   Description: Used to visualize vector data 
	//----------------------------------------------------------------------------------------------------------------
	//			    Status: REFACTORING
	//----------------------------------------------------------------------------------------------------------------
	class MatlabUtilityDebuger
	{
	public:
		/**
		 * Gets the matlab string representation of the samples being shot froma specific point
		 */
		static string CreateVisualizer(vector<ShadeRec*> p_samples, Point3D p_rayOrigin, GeometricObjects* p_currentObject)
		{
			
			stringstream ouptutResult;
			Triangle* currentTriangle = (Triangle*)p_currentObject;

			ouptutResult << "//this is the trinagle coordinates" << "\n";
			ouptutResult << "x = [" <<currentTriangle->point1.vertex.x<<","<<currentTriangle->point2.vertex.x<<","<<currentTriangle->point3.vertex.x<<"]; \n";
			ouptutResult << "y = [" << currentTriangle->point1.vertex.y << "," << currentTriangle->point2.vertex.y << "," << currentTriangle->point3.vertex.y << "]; \n";
			ouptutResult << "z  = [" << currentTriangle->point1.vertex.z << "," << currentTriangle->point2.vertex.z << "," << currentTriangle->point3.vertex.z << "]; \n";

			ouptutResult << "fill3( x, y ,z, 'r'); \n";
			
			ouptutResult << "collisionPoint = [" << p_rayOrigin.x << " " << p_rayOrigin.y << " " << p_rayOrigin.z << "]; \n";

			for (int sampleIndex = 0; sampleIndex < p_samples.size(); sampleIndex++)
			{
				ouptutResult << "sample" << sampleIndex << " = [" << p_samples[sampleIndex]->ray.d.x << " " << p_samples[sampleIndex]->ray.d.y << " " << p_samples[sampleIndex]->ray.d.z << "]; \n";
			}

			ouptutResult << "start = [";

			for (int index = 0; index < p_samples.size(); index++)
			{
				ouptutResult << "collisionPoint" << ";";
			}

			ouptutResult << "]; \n";
			ouptutResult << "ends = [";

			for (int index = 0; index < p_samples.size() ; index++)
			{
				
				//ouptutResult << "sample" + to_string(index) << ";";
			}

			ouptutResult << "]; \n";

			ouptutResult << "quiver3(starts(:,1), starts(:,2), starts(:,3), ends(:,1), ends(:,2), ends(:,3));\n";
			ouptutResult << "xlabel('x'); ylabel('y'); zlabel('z')";

			return ouptutResult.str();
		}

		static string CreateVisualizer(Ray p_currentRay, GeometricObjects* p_currentObject)
		{

			stringstream ouptutResult;
			Triangle* currentTriangle = (Triangle*)p_currentObject;

			ouptutResult << "//this is the trinagle coordinates" << "\n";
			ouptutResult << "x = [" << currentTriangle->point1.vertex.x << "," << currentTriangle->point2.vertex.x << "," << currentTriangle->point3.vertex.x << "]; \n";
			ouptutResult << "y = [" << currentTriangle->point1.vertex.y << "," << currentTriangle->point2.vertex.y << "," << currentTriangle->point3.vertex.y << "]; \n";
			ouptutResult << "z  = [" << currentTriangle->point1.vertex.z << "," << currentTriangle->point2.vertex.z << "," << currentTriangle->point3.vertex.z << "]; \n";

			ouptutResult << "fill3( x, y ,z, 'r'); \n";

			ouptutResult << "collisionPoint = [" << p_currentRay.o.x << " " << p_currentRay.o.y << " " << p_currentRay.o.z << "]; \n";

			
			ouptutResult << "sample1" << " = [" << p_currentRay.d.x << " " << p_currentRay.d.y << " " << p_currentRay.d.z << "]; \n";
			

			ouptutResult << "start = [";

			
				ouptutResult << "collisionPoint" << ";";
		
			ouptutResult << "]; \n";
			ouptutResult << "ends = [";

			ouptutResult << "sample1;";
			

			ouptutResult << "]; \n";

			ouptutResult << "quiver3(starts(:,1), starts(:,2), starts(:,3), ends(:,1), ends(:,2), ends(:,3));\n";
			ouptutResult << "xlabel('x'); ylabel('y'); zlabel('z')";

			return ouptutResult.str();
		}

		/**
		 * Gets the matlab string representation with the quad 
		 */

		/*static string CreateVisualiser(vector<Point3D> p_samples, ShadeRec& startingPoint, Quad* p_currentObject)
		{
			stringstream ouptutResult;
			ouptutResult << "collisionPoint = [" << startingPoint.localHitPoint.x << " " << startingPoint.localHitPoint.y << " " << startingPoint.localHitPoint.z << "]; \n";

			for (int sampleIndex = 0; sampleIndex < p_samples.size(); sampleIndex++)
			{
				ouptutResult << "sample" << sampleIndex << " = [" << p_samples[sampleIndex].x << " " << p_samples[sampleIndex].y << " " << p_samples[sampleIndex].z << "]; \n";
			}

			ouptutResult << "start = [";

			for (int index = 0; index < p_samples.size(); index++)
			{
				ouptutResult << "collisionPoint" << ";";
			}

			ouptutResult << "]; \n";
			ouptutResult << "ends = [";

			for (int index = 0; index < p_samples.size(); index++)
			{
				ouptutResult << "sample" + to_string(index) << ";";
			}

			ouptutResult << "]; \n";

			ouptutResult << "quiver3(starts(:,1), starts(:,2), starts(:,3), ends(:,1), ends(:,2), ends(:,3));\n";
			ouptutResult << "xlabel('x'); ylabel('y'); zlabel('z')";

			ouptutResult << "x = [" << p_currentObject->m_firstTriangle->point1.vertex.x << " " << p_currentObject->m_firstTriangle->point2.vertex.x << " " << p_currentObject->m_firstTriangle->point3.vertex.x << " " << p_currentObject->m_secondTrinagle->point3.vertex.x << "]; \n";
			ouptutResult << "y = [" << p_currentObject->m_firstTriangle->point1.vertex.y << " " << p_currentObject->m_firstTriangle->point2.vertex.y << " " << p_currentObject->m_firstTriangle->point3.vertex.y << " " << p_currentObject->m_secondTrinagle->point3.vertex.y << "]; \n";
			ouptutResult << "z = [" << p_currentObject->m_firstTriangle->point1.vertex.z << " " << p_currentObject->m_firstTriangle->point2.vertex.z << " " << p_currentObject->m_firstTriangle->point3.vertex.z << " " << p_currentObject->m_secondTrinagle->point3.vertex.z << "]; \n";

			ouptutResult << "fill3(x,y,z,'r'); \n";
			ouptutResult << "axis equal \n";

			return ouptutResult.str();
		}*/


		/**
		 * Starts matlab and inputs the samples to the engine 
		 */
		
		static void VisualizeInMatlab()
		{
			//start the engine

			/*engine *m_Engine;
			m_Engine = engOpen("null");

			
			if (m_Engine == NULL)
			{
				std::cout << "Error matlab was not found" << std::endl;
			}*/
		}

	private:

	};
}